<template>
  <div>
    <el-form :model="form" label-width="0" class="login-form">
      <el-form-item label="">
        <el-input v-model="form.username" :placeholder="t('login.phoneNumberOrUsername')">
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="" class="password-item">
        <el-input
          :type="showPassword ? 'text' : 'password'"
          v-model="form.password"
          :placeholder="t('login.password')"
        >
          <template #prefix>
            <el-icon><Lock /></el-icon>
          </template>
          <template #suffix>
            <div class="password-icon cursor-pointer" @click="showPassword = !showPassword">
              <el-icon v-if="showPassword"><View /></el-icon>
              <el-icon v-else><Hide /></el-icon>
            </div>
          </template>
        </el-input>
        <p class="text-right w-full color-#79bbff">
          <span class="text-12px cursor-pointer">{{ t('login.forgetPassword') }}</span>
        </p>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit" class="w-full">{{ t('login.login') }}</el-button>
      </el-form-item>
    </el-form>
    <div>
      <el-divider color="#ebeced">
        <span class="text-12px color-#999">{{ t('login.otherLogin') }}</span>
      </el-divider>
      <div class="flex justify-center items-center gap-20px">
        <i class="iconfont icon-qq cursor-pointer p-10px bg-#f8f8fa text-#50c8fd rounded-50%"></i>
        <i
          class="iconfont icon-weixin cursor-pointer p-10px bg-#f8f8fa text-#00c853 rounded-50%"
        ></i>
      </div>
      <p class="text-12px color-#999 mt-20px">
        {{ t('login.otherLoginDesc') }}
      </p>
    </div>
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { login } from '@/api/login'
import { useUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock, View, Hide } from '@element-plus/icons-vue'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()
const router = useRouter()
const showPassword = ref(false)
const form = ref({
  username: '',
  password: '',
})
const onSubmit = () => {
  login(form.value).then((res: any) => {
    if (res.code === 200) {
      const { token, userInfo } = res.data
      useUserStore().setToken(token)
      useUserStore().setUserInfo(userInfo)
      router.replace('/')
    } else {
      ElMessage.error(res.message)
    }
  })
}
</script>
<style scoped lang="scss"></style>
